/*
 * myusart.c
 *
 *  Created on: Nov 9, 2012
 *      Author: andrey
 */

#include <string.h>
#include "myusart.h"
#include "io.h"
#include "rficonsole.h"
#include "distributor_emulator.h"
#include "rfirtc.h"
#include "rfi_perftest.h"

extern int adcRefreshTime;
extern int adcZoom;
extern int waveDuty;

void runTest() {
	for (int i = 0; i < 10; i++) {
		if (i % 2 == 0) {
//			setDEEnabled(FALSE);
		} else {
//			setDEEnabled(TRUE);
//			setRevolutionPeriod(i * 200);
			//setRevolutionPeriod(i * 200);

		}
		chThdSleepMilliseconds(1000);
	}
}

//void handleInput2(char *token, char *valueStr) {
//	if (strEqual(token, "refresh")) {
//		int value = atoi(valueStr);
//		if (value != ERROR_CODE) {
//			adcRefreshTime = value;
//		}
//	} else if (strEqual(token, "duty")) {
//		int value = atoi(valueStr);
//		if (value != ERROR_CODE) {
//			waveDuty = value;
//		}
//	} else if (strEqual(token, "zoom")) {
//		int value = atoi(valueStr);
//		if (value != ERROR_CODE) {
//			adcZoom = value;
//		}
//	} else if (strEqual(token, "wave")) {
//		int value = atoi(valueStr);
//		if (value != ERROR_CODE) {
////			setWaveFreq(value);
//		}
//	} else if (strEqual(token, "adc")) {
//		int value = atoi(valueStr);
//		if (value != ERROR_CODE) {
//			//setAdcSamplesCount(value);
//		}
//	} else if (strEqual(token, "de")) {
//		print("distributor emulator\r\n");
//
//		if (strEqual(valueStr, "off")) {
//			//setDEEnabled(FALSE);
//		} else if (strEqual(valueStr, "on")) {
////			setDEEnabled(TRUE);
//		} else {
//			int value = atoi(valueStr);
//			if (value != ERROR_CODE)
//				setRevolutionPeriod(value);
////			clearReport();
//		}
//	}
//
//	print("refresh %d\r\n", adcRefreshTime);
//	print("zoom %d\r\n", adcZoom);
////	print("adc %d\r\n", getAdcSamplesCount());
//	print("de %d\r\n", getRevolutionPeriod());
////	print("de enabled %d\r\n", getDEEnabled());
//	//print("wave %d\r\n", getWaveFreq());
//	print("duty %d\r\n", waveDuty);
////	print("wave delay %d\r\n", getWaveLength());
//}

//	chHeapInit(&heap1, HEAP1, sizeof(HEAP1));
//	char* input = chHeapAlloc(&heap1, 128);

static void cmd_mem() {
	size_t n, size;

	n = chHeapStatus(NULL, &size);
	print("core free memory : %u bytes\r\n", chCoreStatus());
	print("heap fragments   : %u\r\n", n);
	print("heap free total  : %u bytes\r\n", size);
}

void testTimePerf(int count) {
	testMath(count);
	testSystemCalls(count);
	time_t start, time;
	start = currentTimeMillis();
	for (int i = 0; i < count; i++)
		setPinValue(DE_IO_PORT, DE_Y_BL_WIRE_PIN, i % 2);
	time = currentTimeMillis() - start;
	print(
			"Finished %d iterations of setPinValue(on and off) division in %dms\r\n",
			count, time);

	start = currentTimeMillis();
	for (int i = 0; i < count; i++)
		setPinValue(DE_IO_PORT, DE_Y_BL_WIRE_PIN, 1);
	time = currentTimeMillis() - start;
	print("Finished %d iterations of setPinValue(1) division in %dms\r\n",
			count, time);

	count /= 10;
	// slower operations go here

	start = currentTimeMillis();
	for (int i = 0; i < count; i++)
		chThdSleep(1);
	time = currentTimeMillis() - start;
	print("Finished %d iterations of 'chThdSleep(1)' in %dms\r\n", count, time);
}

void initUsart() {
	addConsoleAction1("clock", &testTimePerf);

	initConsole();

}
